package com.bw.gmall.realtime.app.dwd;

import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.app.func.DimAsyncFunction;
import com.bw.gmall.realtime.utils.DateFormatUtil;
import com.bw.gmall.realtime.utils.MyKafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.concurrent.TimeUnit;

public class Dwd_Shop_not_VisitUser {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> dataStreamSource = env.addSource(MyKafkaUtil.getFlinkKafkaConsumer("dwd_shop_new_visit", "dwd_shop_new_visitaa"));
        dataStreamSource.print();
        SingleOutputStreamOperator<JSONObject> map = dataStreamSource.map(t -> JSONObject.parseObject(t));
        KeyedStream<JSONObject, Tuple2<String, String>> keyBy = map.keyBy(new KeySelector<JSONObject, Tuple2<String, String>>() {
            @Override
            public Tuple2<String, String> getKey(JSONObject value) throws Exception {
                String shop = value.getJSONObject("page").getString("shop");
                String mid = value.getJSONObject("common").getString("mid");
                return Tuple2.of(mid, shop);
            }
        });
        SingleOutputStreamOperator<JSONObject> process = keyBy.process(new KeyedProcessFunction<Tuple2<String, String>, JSONObject, JSONObject>() {
            ValueState<String> dta;

            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                dta = getRuntimeContext().getState(new ValueStateDescriptor<String>("dta", String.class));
            }

            @Override
            public void processElement(JSONObject value, KeyedProcessFunction<Tuple2<String, String>, JSONObject, JSONObject>.Context ctx, Collector<JSONObject> out) throws Exception {
                String value1 = dta.value();
                Long ts = value.getLong("ts");
                String s = DateFormatUtil.toDate(ts);
                if (value1 == null || !value1.equals(s)) {
                    dta.update(s);
                    out.collect(value);
                }
            }
        });
        process.map(t->t.toJSONString()).addSink(MyKafkaUtil.getFlinkKafkaProducer("" +
                "" +
                "" +
                "" +
                "" +
                "" +
                "" +
                ""));

        env.execute();
    }
}
